#include <iostream>
#include <string>
#include <vector>
#include <string.h>
using namespace std;
int main() {
  // 1. 方程的解析，字符串的处理
  // 2. a, b, c
  // 3. [L, R] 正整数符合条件，i
  int n, q;
  cin >> n >> q;
  vector<int> a, b, c;
  while (n --) {
    string str;
    cin >> str;
    // -a -b c
    int plus = str.find("x");
    int equal = str.find("=");
    a.push_back(stoi(str.substr(0, plus)));
    b.push_back(stoi(str.substr(plus + 1, equal - plus - 1)));
    c.push_back(stoi(str.substr(equal + 1)));
    // char c_str[12];
    // strcpy(c_str, str.c_str());
    // char* token = strtok(c_str, "x=");
    // a.push_back(atoi(token));
    // token = strtok(NULL, "x=");
    // b.push_back(atoi(token));
    // token = strtok(NULL, "x=");
    // c.push_back(atoi(token));
  }
  // for (auto & x : a) cout << x << ' ';
  // cout << endl;
  // for (auto & x : b) cout << x << ' ';
  // cout << endl;
  // for (auto & x : c) cout << x << ' ';
  // cout << endl;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int cnt = 0;
    for (int x = l; x <= r; x++) {
      for (int i = 0; i < a.size(); i ++) {
        if (a[i] * x + b[i] == c[i]) {
          cnt ++;
          break;
        }
      }
    }
    cout << cnt << endl;
  }
}